/** js 分页 */
/**
 * js分页，从 pager_ui.ftl 翻译成js语句
 * @param element 分页放置位置
 * @param page 当前页号(int类型)
 * @param pageSize 每页要显示的记录数(int类型)
 * @param totalRecords 总记录数(int类型)
 * @param callback 分页后回调
 * @param type 分页类型  0 正常情况 1 带跳转功能 2 可以切换每页显示数目
 * @constructor
 */
;(function ($, window) {
    var JsPager = function (options) {
        var defaultSetting = {
            element: undefined,
            page: 1,
            pageSize: 10,
            showJump: true,
            pageSizeList: [10, 20, 50],
            pageChangeCallback: undefined,
            /*1.正常，3.hbase*/
            model: 1
        };
        /*事件类型，事件按照类型进行绑定*/
        this.options = $.extend(true, {}, defaultSetting, options);
        this.options.pageSizeList = options.pageSizeList;
        this.pageSize = this.options.pageSize;
        this.page = this.options.page;
    };
    JsPager.prototype = {
        setPage: function setPage(page, pageSize, totalRecords, canNext) {
            this.page = page;
            this.pageSize = pageSize;
            if (this.page < 1) {
                this.page = 1;
            }
            this.totalRecords = totalRecords;
            this.totalPage = parseInt(this.totalRecords / this.pageSize);
            if (this.totalRecords % this.pageSize != 0) {
                this.totalPage++;
            }
            if (this.page > this.totalPage) {
                this.page = this.totalPage;
            }
            if (this.options.model == 3) {
                this.renderHbase(canNext);
            } else {
                this.render();
            }
        },
        render: function () {
            var _this = this;
            _this.pageEl = $("<div class='page fr'></div>");
            var prevBtn = $("<a href='javascript:void(0)' class='prev'>«</a>");
            if (this.page == 1) {
                prevBtn.addClass("notall");
            } else {
                prevBtn.click(function () {
                    _this.goPrePage();
                })
            }
            _this.pageEl.append(prevBtn);
            var start = 1;
            if (this.page > 4) {
                start = this.page - 1;
                _this.pageEl.append(this.renderPageBtn(1));
                _this.pageEl.append(this.renderPageBtn(2));
                _this.pageEl.append($("<span>...</span>"));
            }
            var end = this.page + 1;
            if (end > this.totalPage) {
                end = this.totalPage;
            }
            /*中间部分*/
            for (var i = start; i <= end; i++) {
                var nowBtn
                if (this.page == i) {
                    nowBtn = $("<b></b>");
                    nowBtn.text(i);
                } else {
                    nowBtn = this.renderPageBtn(i);
                }
                _this.pageEl.append(nowBtn);
            }
            if (end < this.totalPage - 2) {
                _this.pageEl.append($("<span>...</span>"));
            }
            if (end < this.totalPage - 1) {
                _this.pageEl.append(this.renderPageBtn(this.totalPage - 1));
            }
            if (end < this.totalPage) {
                _this.pageEl.append(this.renderPageBtn(this.totalPage));
            }
            var nextBtn = $("<a href='javascript:void(0)' class='next'>»</a>");
            if (this.page == this.totalPage) {
                nextBtn.addClass("notall")
            } else {
                nextBtn.click(function () {
                    _this.goNextPage();
                });
            }
            _this.pageEl.append(nextBtn);
            /*自由跳转功能*/
            _this.renderFreeJump();
            /*调整分页大小*/
            _this.renderPageSelect();
            this.options.element.empty();
            this.options.element.append(_this.pageEl);
        },
        renderFreeJump: function () {
            if (this.options.showJump) {
                var _this = this;
                var freeJumpEl = $("<div class='page-inp'><span>跳至</span></div>");
                var freeJumpInput = $('<input class="inp" type="text" value=""><span>页</span>');
                var freeJumpBtn = $('<input class="submit" type="submit" value="跳转">');
                freeJumpBtn.click(function () {
                    var pageValue = parseInt(freeJumpInput.val());
                    if (pageValue) {
                        _this.goPage(pageValue);
                    }
                });
                freeJumpInput.keypress(function (e) {
                    e = e || window.event;
                    key = e.keyCode || e.which || e.charCode;
                    var pageValue = parseInt(freeJumpInput.val());
                    if (key == 13) {
                        _this.goPage(pageValue);
                    }
                });
                freeJumpEl.append(freeJumpInput);
                freeJumpEl.append(freeJumpBtn);
                _this.pageEl.append(freeJumpEl);
            }
        },
        renderPageSelect: function () {
            var _this = this;
            if (this.options.pageSizeList && this.options.pageSizeList.length > 1) {
                var pageSizeSelectEl = $("<div class='page-inp-pre'>每页展示</div>")
                $(this.options.pageSizeList).each(function (index, pageSize) {
                    var el = $("<a href='javascript:void(0)'></a>").text(pageSize);
                    if (_this.pageSize == pageSize) {
                        el.addClass("current");
                    }
                    el.click(function () {
                        _this.setPage(1, pageSize, _this.totalRecords);
                        _this.goPage(1);
                    });
                    pageSizeSelectEl.append(el);
                });
                pageSizeSelectEl.append('<div class="total-page">共<span>' + this.totalRecords + '</span>条记录</div>');
                _this.pageEl.prepend(pageSizeSelectEl);
            }else{
                _this.pageEl.prepend('<div class="page-inp-pre"><div class="total-page">共<span>' + this.totalRecords + '</span>条记录</div></div>');
            }
        },
        renderPageBtn: function (page) {
            var btn = $("<a href='javascript:void(0)'></a>");
            btn.text(page);
            var _this = this;
            btn.click(function () {
                _this.goPage(page);
            });
            return btn;
        },
        goPage: function (page) {
            if (this.options.model == 3) {
                if (page < 1) {
                    return;
                }
                this.page = page;
                if (this.options.pageChangeCallback) {
                    this.options.pageChangeCallback(this.page);
                } else {
                    this.renderHbase(true);
                }
            } else {
                if (page < 1) {
                    page = 1;
                }
                if (page > this.totalPage) {
                    page = this.totalPage;
                }
                if (page > this.totalPage) {
                    page = this.totalPage;
                }
                this.page = page;
                if (this.options.pageChangeCallback) {
                    this.options.pageChangeCallback(this.page);
                } else {
                    this.render();
                }
            }

        },
        goPrePage: function goPrePage() {
            var page = this.page - 1;
            if (page < 1) {
                return;
            }
            if (page > this.totalPage) {
                return;
            }
            this.goPage(page);
        },
        goNextPage: function goNextPage() {
            var page = this.page + 1;
            if (this.options.model != 3 && page > this.totalPage) {
                return;
            }
            this.goPage(page);
        },
        renderHbase: function (canNext) {
            var _this = this;
            var elePages = $("<div class='pages fr'></div>");

            var preElement = $("<a href='javascript:void(0)'>上一页</a>");
            // 上一页处理
            if (this.page > 1) {
                preElement.addClass("medium ui blue button")
                preElement.click(function () {
                    _this.goPrePage();
                });
            } else {
                preElement.addClass("medium ui blue button disabled")
            }
            elePages.append(preElement);
            // 下一页处理
            var nextElement = $("<a href='javascript:void(0)'>下一页</a>");

            if (canNext) {
                nextElement.addClass('medium ui blue button');
                nextElement.click(function () {
                    _this.goNextPage();
                });
            } else {
                nextElement.addClass('medium ui blue button disabled');
            }
            elePages.append(nextElement);
            this.options.element.empty();
            this.options.element.append(elePages);
        }
    };
    window.jPage = function (options) {
        return new JsPager(options);
    };
}(jQuery, window));
